<template>
  <view class="content">
    <image
      class="w-100"
      src="https://digital.essc.top/dist/zjl_icon/logo.png"
      mode=""
    ></image>
    <view class="p-2 flex flex-column align-center">
      <text class="my-2 font-weight-bold font-lg">使用账号密码登录</text>
      <uni-forms
        :rules="rules"
        ref="Form"
        :modelValue="FormData"
        label-position="top"
      >
        <uni-forms-item name="username" label="账号" required>
          <uni-easyinput
            v-model.trim="FormData.username"
            placeholder="请输入手机号"
          />
        </uni-forms-item>
        <uni-forms-item name="password" label-width="300" label="密码" required>
          <uni-easyinput
            type="password"
            v-model.trim="FormData.password"
            placeholder="请输入密码"
          />
        </uni-forms-item>
      </uni-forms>
      <view class="foot">
        <button
          :disabled="disabled"
          @click="Login('Form')"
          type="primary"
          size="mini"
        >
          登陆
        </button>
        <button
          @click="goRegistered('/pages/My/Registered')"
          size="mini"
          type="primary"
        >
          去注册
        </button>
      </view>
    </view>
    <view class="font flex p-4 box-sizing">
      <checkbox @click="updateCheckValue" />
      <view
        >登录即代表您同意<text
          :class="'text-' + color"
          @click="goRegistered('/pages/My/Aboutus/User_Service_Agreement')"
          >《用户服务协议》</text
        ><text
          :class="'text-' + color"
          @click="
            goRegistered('/pages/My/Aboutus/User_Authorization_Agreement')
          "
          >《用户授权协议》</text
        ><text
          :class="'text-' + color"
          @click="goRegistered('/pages/My/Aboutus/Privacy_Policy')"
          >《隐私政策》</text
        >
      </view>
    </view>
  </view>
</template>

<script>
import times from "@/mixins/time.js";
const app = getApp();
export default {
  name: "login",
  data() {
    return {
      color: uni.getStorageSync("ThemeColors")
        ? uni.getStorageSync("ThemeColors")
        : "green",
      header: "",
      checkvalue: 0,
      disabled: false,
      FormData: {
        username: "",
        password: "",
        url: "/pages/index/index",
        client: 1,
      },
      LoginUrl: "",
      rules: {
        username: {
          rules: [
            {
              required: true,
              errorMessage: "请输入手机号",
            },
            {
              format: "number",
              errorMessage: "手机号只能输入数字",
            },
            // {
            // 	pattern: /^1[3-9]\d{9}$/,
            // 	errorMessage: '手机号不合法！'
            // },
          ],
        },
        password: {
          rules: [
            {
              required: true,
              errorMessage: "请输入密码",
            },
            {
              minLength: 6,
              errorMessage: "密码至少为6位",
            },
          ],
        },
      },
    };
  },
  methods: {
    updateCheckValue() {
      if (this.checkvalue == 0) {
        this.checkvalue = 1;
      } else {
        this.checkvalue = 0;
      }
    },
    Login(ref) {
      if (this.checkvalue == 0) {
        uni.showToast({
          title: "请勾选协议",
          icon: "none",
        });
        return;
      }
      this.$refs[ref]
        .validate()
        .then((res) => {
          let _this = this;
          this.disabled = true;
          uni.showLoading({
            title: "登录中...",
            mask: true,
          });
          const header = {
            "Content-Type": "application/x-www-form-urlencoded",
          };

          uni.request({
            url: app.globalData.Domain_Name + "/OpenURLAction",
            header: header,
            data: this.FormData,
            method: "POST",
            success: (res) => {
              // 处理登录成功的逻辑
              console.log(res.cookies);
              _this.disabled = false;
                const loginInfo2 = {
                  token: null,
                  enterprise: null,
                  username: null,
                  use: null,
                  authentication: null,
                  authenticationType: null,
                };
                loginInfo2.token = res.header.COOKIES,
                loginInfo2.enterprise = res.header.ENTERPRISE,
                loginInfo2.username = decodeURI(res.header.USERNAME),
                loginInfo2.use = decodeURI(res.header.USE),
                loginInfo2.authentication =  decodeURI(res.header.AUTHENTICATION),
                loginInfo2.authenticationType = decodeURI(res.header.AUTHENTICATIONTYPE)
                uni.setStorageSync("ZJl_use", loginInfo2);
                //
                uni.setStorageSync("token", res.header.COOKIES);
                uni.setStorageSync("enterprise", res.header.ENTERPRISE);
                uni.setStorageSync("username", decodeURI(res.header.USERNAME));
                uni.setStorageSync("use", decodeURI(res.header.USE));
                uni.setStorageSync(
                  "authentication",
                  decodeURI(res.header.AUTHENTICATION)
                );
                uni.setStorageSync(
                  "authenticationType",
                  decodeURI(res.header.AUTHENTICATIONTYPE)
                );
            },
            complete(res) {
              let mintes = 1000 * 1 * 60;
              uni.setStorageSync("mintes", mintes);
              uni.setStorageSync("nowtime", times.nowtime());
              uni.hideLoading();
              if (res.header.RESUIT == "ERROR") {
                uni.showToast({
                  title: "密码错误",
                  icon: "error",
                });
              } else {
                uni.showToast({
                  title: "登陆成功",
                  icon: "success",
                });
                if (_this.LoginUrl != "") {
                  uni.navigateTo({
                    url: _this.LoginUrl,
                  });
                } else {
                  setTimeout(() => {
                    uni.switchTab({
                      url: "/pages/index/index",
                    });
                  }, 2000);
                }
                uni.hideLoading();
              }
            },
            fail: (err) => {
              // 处理登录失败的逻辑
              console.error(err);
            },
          });
        })
        .catch((err) => {
          console.log(err);
        });
      // uni.addInterceptor('request', { // 请求拦截器
      //  invoke: function(config) { console.log(config); // 输出请求头信息
      //  return config; } });
    },
    CheckChange(e) {
      if (e.detail.value.length == 0) {
        this.checkvalue = "0";
      } else {
        this.checkvalue = "1";
      }
    },
    goRegistered(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
uni-page-body {
  height: 100%;
}

.content {
  background-color: #fff;
  height: 100%;

  /deep/ .uni-forms {
    width: 100%;
  }
}

.foot {
  display: flex;
  width: 100%;

  uni-button {
    color: #fff;
  }
}

.uni-forms {
  width: 100%;
}

.infoActive {
  border-width: 4rpx;
  border-style: solid;
  transition: all 0.5s;
}

.xieyicol {
  color: #fc7c08;
}

// /deep/ .uni-easyinput {
//   width: 100% !important;
// }
</style>